package app.modules.infrastructure.lov.application.web;

import app.modules.common.dto.web.WebIdDTO;
import app.modules.common.util.web.PageUtil;
import app.modules.infrastructure.lov.business.service.ValueSetService;
import app.modules.infrastructure.lov.dto.web.WebValueSetDTO;
import app.modules.infrastructure.lov.dto.web.WebValueSetQueryDTO;
import app.modules.infrastructure.lov.dto.web.WebValueSetSaveDTO;
import app.modules.infrastructure.lov.dto.web.WebValueSetUpdateDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import jasmine.framework.web.entity.WebResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author mh.z
 */
@RestController
public class ValueSetController {
    private ValueSetService valueSetService;

    public ValueSetController(ValueSetService valueSetService) {
        this.valueSetService = valueSetService;
    }

    @Operation(summary = "分页查询值集")
    @PostMapping("/api/infrastructure/lov/pageValueSetsByCond")
    public ResponseEntity<WebResult<List<WebValueSetDTO>>> pageWebValueSetDTOsByCond(
            @RequestBody WebValueSetQueryDTO queryDTO) {
        Page page = PageUtil.getPage(queryDTO);

        List<WebValueSetDTO> recordList = valueSetService.pageWebValueSetDTOsByCond(queryDTO, page);
        WebResult result = PageUtil.getResult(recordList, page);

        return result.toOkEntity();
    }

    @Operation(summary = "查询指定 ID 的值集")
    @PostMapping("/api/infrastructure/lov/getValueSetById")
    public ResponseEntity<WebResult<WebValueSetDTO>> getWebValueSetDtoById(
            @RequestBody WebIdDTO dto) {
        Long id = dto.getId();
        WebValueSetDTO recordDTO = valueSetService.getWebValueSetDtoById(id);

        return WebResult.success(recordDTO).toOkEntity();
    }

    @Operation(summary = "保存值集")
    @PostMapping("/api/infrastructure/lov/saveValueSet")
    public ResponseEntity<WebResult<WebValueSetDTO>> saveValueSet(
            @Valid @RequestBody WebValueSetSaveDTO saveDTO) {
        WebValueSetDTO recordDTO = valueSetService.saveValueSet(saveDTO);
        WebResult result = WebResult.success(recordDTO);

        return result.toOkEntity();
    }

    @Operation(summary = "更新值集")
    @PostMapping("/api/infrastructure/lov/updateValueSet")
    public ResponseEntity<WebResult<WebValueSetDTO>> updateValueSet(
            @Valid @RequestBody WebValueSetUpdateDTO updateDTO) {
        WebValueSetDTO recordDTO = valueSetService.updateValueSet(updateDTO);
        WebResult result = WebResult.success(recordDTO);

        return result.toOkEntity();
    }

}
